//获取管理员列表
var table = $("#staff_List").dataTable({
    //"iDisplayLength":10,
    "ajax": {
        "url":"/system/get-biz-list",
        "type":'post',
        "data": function (d) {
            //添加额外的参数传给服务器
            //return $.extend({ CurrentPage:0, PageSize: 10}, {});
        }},
    'bPaginate': true,
    "bDestory": true,
    "bRetrieve": true,
    "bFilter":false,
    "bSort": true,
    "bProcessing": true,
    "autoWidth": false,
    "searching":true,
    "aoColumns": [
        {"mDataProp": "BizKey",
            "fnCreatedCell": function (nTd, sData, oData, iRow, iCol) {
                $(nTd).html(iRow+1);
            }
        },
        {"mDataProp": "BizKey"},
        {"mDataProp": "KeyCode"},
        {"mDataProp": "KeyName"},
        {"mDataProp": "BizVal"},
        {"mDataProp": "Comment"},
        {
            "mDataProp": "BizKey",
            "fnCreatedCell": function (nTd, sData, oData, iRow, iCol) {
                $(nTd).html('<button type="button" class="btn btn-info btn-xs EcitBiz"  data-bizname="'+oData.KeyName+'"  data-bizkey="'+oData.BizKey+'"  data-bizval="'+oData.BizVal+'" data-bizcom = "'+oData.Comment+'"><i class="fa fa-edit"></i> 编辑</button>')
            }
        },
    ],
    "order": [[0, "desc" ]],
    "fnCreatedRow": function (nRow, aData, iDataIndex) {
        //add selected class
        $(nRow).click(function () {
            if ($(this).hasClass('row_selected')) {
                $(this).removeClass('row_selected');
            } else {
                table.$('tr.row_selected').removeClass('row_selected');
                $(this).addClass('row_selected');
            }
        });
    },
    "fnInitComplete": function (oSettings, json) {
    }
});


//点击编辑
$(document).on('click','.EcitBiz',function(){
    var BizKey = $(this).data("bizkey");
    var BizVal = $(this).data("bizval");
    var BizName = $(this).data("bizname");
    var Comment = $(this).data("bizcom");
    $("#BizVal").val(BizVal);
    $("#Comment").val(Comment);
    $("#EditForm").attr("data-bizkey",BizKey).attr("data-bizname",BizName);
   $("#EditBiz").modal('show');
});

//编辑
function EditBiz(){
    var JsonData = {
        BizKey:$("#EditForm").data("bizkey"),
        BizName :$("#EditForm").data("bizname"),
        BizVal: $("#BizVal").val(),
        Comment: $("#Comment").val()
    }
    $.ajax({
        type: "post",
        dataType: "json",
        url: '/system/biz-update',
        data: JsonData,
        success: function (ret) {
            $("#EditBiz").modal('hide');
            resetFrom();
            if(ret.IsBizSuccess){
                var table = $('#staff_List').DataTable();
                table.ajax.reload();
                layer.msg('修改成功！');
            }else{
                layer.msg('修改失败！');
            }
        }
    });
}


//隐藏模态窗口
$("#EditBiz").on("hide.bs.modal", function () {
    setTimeout(function(){
        resetFrom();
    },100)

});
$("#EditForm").validate({
    submitHandler:function() {
        EditBiz();
    }
});



//重置表单
function resetFrom() {
    $('form').each(function (index) {
        $('form')[index].reset();
    });
}